home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Util / ListSTAR.c < prev    next >
Text File  |  1996-04-11  |  9KB  |  313 lines

  1. /*****
  2.  *
  3.  *    Grant's CGI Framework
  4.  *        (Common Grant Interface :-)
  5.  *        by Grant Neufeld
  6.  *        http://arpp.carleton.ca/grant/mac/grantscgi/
  7.  *
  8.  *    ListSTAR.c
  9.  *
  10.  *    Standard functions for ListSTAR trigger & action applications.
  11.  *
  12.  *    You must call InitListSTARUtil in your application startup.
  13.  *    You must install ListSTARAEHandle as the event handler for the 9evt9Lis apple event
  14.  *    You must write the function:
  15.  *        void CustomListSTARProcess ( LStarHdl theLStarHdl )
  16.  *        which is where you will, guess what, do your application specific processing
  17.  *        of the ListSTAR stuff.
  18.  *
  19.  *    Do not call any functions begining with lower case 'lstar' - you can use any of the
  20.  *    others - but read their comments first for details.
  21.  *
  22.  *    watch the homepage for future upgrades
  23.  *
  24.  *    notice of upgrades will be posted to macwwwtool@arpp1.carelton.ca
  25.  *        see http://arpp.carleton.ca/list/macwwwtool.html for details
  26.  *
  27.  *
  28.  *    Copyright ©1995,1996 by Grant Neufeld
  29.  *
  30.  *    http://arpp.carleton.ca/grant/
  31.  *    gneufeld@ccs.carleton.ca
  32.  *    grant@acm.org
  33.  *
  34.  *    This source may be freely used as long as the copyright notice is kept in the source.
  35.  *    I ask that you let me know of any enhancements (read: bug fixes) to this code.
  36.  *    I would also like copies of (or discounts on) anything you produce this with, please.
  37.  *
  38.  *    See the License and Limited Warranty Agreement for all the legal stuff.
  39.  *
  40.  *****/
  41.  
  42. #include "MyConfiguration.h"
  43. #if kCompileWithListSTARCode
  44.  
  45.  
  46. #include <string.h>
  47.  
  48. #include "compiler_stuff.h"
  49. #include "globals.h"
  50.  
  51. #include "AEFunc.h"
  52. #include "DebugUtil.h"
  53. #include "MemoryUtil.h"
  54. #include "StringUtil.h"
  55.  
  56. /* ListSTAR.h processes differently for ListSTAR.c,
  57.     this is controlled by defining __LStarSegment__ */
  58. #define __LStarSegment__    1
  59. #include "ListSTAR.h"
  60. #undef    __LStarSegment__
  61.  
  62.  
  63. /***  CONSTANT DECLARATIONS  ***/
  64.  
  65. /***  LOCAL FUNCTION PROTOTYPES  ***/
  66.  
  67. static    void    lstarDisposeHandle            ( LStarHdl );
  68.  
  69. static    OSErr    lstarAEGetParamSpoolFile    ( AppleEvent *, AEKeyword, FSSpec *, char *, long );
  70.  
  71.  
  72. /***  FUNCTIONS  ***/
  73.  
  74. /* This initialization function MUST be called in the startup sequence of
  75.     your application */
  76. OSErr
  77. InitListSTARUtil ( void )
  78. {
  79.     OSErr                theErr;
  80.     AEEventHandlerUPP    theUPP;
  81.     StringHandle        liststarSpoolPath;
  82.     
  83.     /* install the ListSTAR apple event handler */
  84.     theUPP    = NewAEEventHandlerProc ( ListSTARAppleEvent );
  85.     theErr    = AEInstallEventHandler ( kAEClassStarNine, kAEIDListStarEvent, theUPP, 0L, false );
  86.     if ( theErr != noErr )
  87.     {
  88.         /* liststar AE install failed */
  89.         return theErr;
  90.     }
  91.     
  92.     liststarSpoolPath = GetString ( krsListSTARSpoolPath );
  93.     if ( liststarSpoolPath != NULL )
  94.     {
  95.         HLock ( (Handle)liststarSpoolPath );
  96.         
  97.         /* locate ListSTAR's mail spool folder and set up it's FSSpec.
  98.             OSErr returned ignored */
  99.         theErr = FSMakeFSSpec ( nil, nil, *liststarSpoolPath, &gListSTARSpoolFolder );
  100.         
  101.         HUnlock ( (Handle)liststarSpoolPath );
  102.         ReleaseResource ( (Handle)liststarSpoolPath );
  103.     }
  104.     
  105.     return theErr;
  106. } /* InitListSTARUtil */
  107.  
  108.  
  109. #pragma mark -
  110.  
  111. /*  */
  112. static void
  113. lstarDisposeHandle ( LStarHdl theLStarHdl )
  114. {
  115.     my_assert ( theLStarHdl != NULL, "\plstarDisposeHandle: theLStarHdl is NULL" );
  116.     
  117.     if ( (*theLStarHdl)->mailService != NULL )
  118.     {
  119.         DisposePtr ( (Ptr)((*theLStarHdl)->mailService) );
  120.     }
  121.     if ( (*theLStarHdl)->senderName != NULL )
  122.     {
  123.         DisposePtr ( (Ptr)((*theLStarHdl)->senderName) );
  124.     }
  125.     if ( (*theLStarHdl)->senderEmail != NULL )
  126.     {
  127.         DisposePtr ( (Ptr)((*theLStarHdl)->senderEmail) );
  128.     }
  129.     if ( (*theLStarHdl)->mailSubject != NULL )
  130.     {
  131.         DisposePtr ( (Ptr)((*theLStarHdl)->mailSubject) );
  132.     }
  133.     if ( (*theLStarHdl)->regExp != NULL )
  134.     {
  135.         DisposePtr ( (Ptr)((*theLStarHdl)->regExp) );
  136.     }
  137.     if ( (*theLStarHdl)->senderMC != NULL )
  138.     {
  139.         DisposePtr ( (Ptr)((*theLStarHdl)->senderMC) );
  140.     }
  141.     if ( (*theLStarHdl)->senderMCZone != NULL )
  142.     {
  143.         DisposePtr ( (Ptr)((*theLStarHdl)->senderMCZone) );
  144.     }
  145.     if ( (*theLStarHdl)->senderMCEmail != NULL )
  146.     {
  147.         DisposePtr ( (Ptr)((*theLStarHdl)->senderMCEmail) );
  148.     }
  149.     
  150.     DisposeHandle ( (Handle)theLStarHdl );
  151. } /* lstarDisposeHandle */
  152.  
  153.  
  154. /**  APPLE EVENT SUPPORT  **/
  155. #pragma mark -
  156.  
  157. #pragma segment AppleEvents
  158. /* AppleEvent Handler for the ListSTAR 9evt-9Lis event */
  159. pascal OSErr
  160. ListSTARAppleEvent ( AppleEvent *theAppleEvent, AppleEvent *TheReply, long Reference )
  161. {
  162.     OSErr        theErr;
  163.     Ptr            tempBuffer;
  164.     LStarHdl    theLStarHdl;
  165.     
  166.     theLStarHdl = (LStarHdl) MemoryNewHandleClear ( sizeof(LStarRecord), &theErr );
  167.     if ( theLStarHdl == NULL )
  168.     {
  169.         return theErr;
  170.     }
  171.     
  172.     /* store references to the apple event and reply records */
  173.     (*theLStarHdl)->appleEvent = theAppleEvent;
  174.     (*theLStarHdl)->replyEvent = TheReply;
  175.     
  176.     tempBuffer = MemoryNewPtr ( kLStarParamMaxSize, &theErr );
  177.     if ( tempBuffer == NULL )
  178.     {
  179.         DisposeHandle ( (Handle)theLStarHdl );
  180.         
  181.         return theErr;
  182.     }
  183.     
  184.     my_assert ( (HGetState((Handle)theLStarHdl) & kMemoryHandleLockedFlag) == nil,
  185.         "\pListSTARAppleEvent: theLStarHdl is already locked!" );
  186.     HLockHi ( (Handle)theLStarHdl );
  187.     
  188.     /* '----' - direct parameter: mailService */
  189.     theErr = AEGetParamString ( theAppleEvent, kLSTARmailService, &((*theLStarHdl)->mailService),
  190.         (char *)tempBuffer, kLStarParamMaxSize );
  191.     
  192.     /* 's9nm' - senderName */
  193.     theErr = AEGetParamString ( theAppleEvent, kLSTARsenderName, &((*theLStarHdl)->senderName),
  194.         (char *)tempBuffer, kLStarParamMaxSize );
  195.     
  196.     /* 's9em' - senderEmail */
  197.     theErr = AEGetParamString ( theAppleEvent, kLSTARsenderEmail, &((*theLStarHdl)->senderEmail),
  198.         (char *)tempBuffer, kLStarParamMaxSize );
  199.     
  200.     /* 's9sj' - mailSubject */
  201.     theErr = AEGetParamString ( theAppleEvent, kLSTARmailSubject, &((*theLStarHdl)->mailSubject),
  202.         (char *)tempBuffer, kLStarParamMaxSize );
  203.     
  204.     /* 's9re' - regExp */
  205.     theErr = AEGetParamString ( theAppleEvent, kLSTARregExp, &((*theLStarHdl)->regExp),
  206.         (char *)tempBuffer, kLStarParamMaxSize );
  207.     
  208.     /* 's9fn' - mailFile */
  209.     theErr = lstarAEGetParamSpoolFile ( theAppleEvent, kLSTARmailFile, &((*theLStarHdl)->mailFile),
  210.         (char *)tempBuffer, kLStarParamMaxSize );
  211.     
  212.     /* 's9mc' - senderMC */
  213.     theErr = AEGetParamString ( theAppleEvent, kLSTARsenderMC, &((*theLStarHdl)->senderMC),
  214.         (char *)tempBuffer, kLStarParamMaxSize );
  215.     
  216.     /* 's9mz' - senderMCZone */
  217.     theErr = AEGetParamString ( theAppleEvent, kLSTARsenderMCZone, &((*theLStarHdl)->senderMCZone),
  218.         (char *)tempBuffer, kLStarParamMaxSize );
  219.     
  220.     /* 's9me' - senderMCEmail */
  221.     theErr = AEGetParamString ( theAppleEvent, kLSTARsenderMCEmail, &((*theLStarHdl)->senderMCEmail),
  222.         (char *)tempBuffer, kLStarParamMaxSize );
  223.     
  224.     /* don't need the buffer any more */
  225.     DisposePtr ( tempBuffer );
  226.     
  227.     /* ••• do we need to add "MyAEGotRequiredParams" here?? */
  228.     
  229.     /* don't know how to determine difference between action and trigger yet,
  230.         so default to trigger. This can be changed to action by the
  231.         'CustomListSTARProcess' function */
  232.     (*theLStarHdl)->method = ListSTAR_Trigger;
  233.     
  234.     /* allocate triggerValue */
  235.     (*theLStarHdl)->triggerValue = (long *) MemoryNewPtr ( sizeof(long), NULL );
  236.     if ( (*theLStarHdl)->triggerValue != NULL )
  237.     {
  238.         /* default to failure unless the 'CustomListSTARProcess' function determines success or error */
  239.         *((*theLStarHdl)->triggerValue)    = Trigger_failure;
  240.     }
  241.     
  242.     /* this is where the application specific ListSTAR event handling comes into play
  243.         the function "CustomListSTARProcess" must be provided by the user of
  244.         this source code */
  245.     HUnlock ( (Handle)theLStarHdl );
  246.     ProcessIsMoreBusy ();
  247.     CustomListSTARProcess ( theLStarHdl );
  248.     ProcessIsLessBusy ();
  249.     
  250.     my_assert ( (HGetState((Handle)theLStarHdl) & kMemoryHandleLockedFlag) == nil,
  251.         "\pListSTARAppleEvent: theLStarHdl is already locked!" );
  252.     HLock ( (Handle)theLStarHdl );
  253.     
  254.     if ( ((*theLStarHdl)->method == ListSTAR_Trigger) &&
  255.         ((*theLStarHdl)->triggerValue != NULL) )
  256.     {
  257.         /* if the event is a trigger, return the trigger value */
  258.         theErr = AEPutParamPtr ( TheReply, keyDirectObject, typeLongInteger,
  259.             (Ptr)((*theLStarHdl)->triggerValue), sizeof(long) );
  260.     }
  261.     else
  262.     {
  263.         /* otherwise, don't return any data */
  264.         theErr = noErr;
  265.     }
  266.     
  267.     HUnlock ( (Handle)theLStarHdl );
  268.     
  269.     /* deallocate memory */
  270.     lstarDisposeHandle ( theLStarHdl );
  271.     
  272.     return theErr;
  273. } /* ListSTARAppleEvent */
  274.  
  275.  
  276. #pragma segment AppleEvents
  277. /* private function to get an HTTPMethod from a parameter */
  278. static OSErr
  279. lstarAEGetParamSpoolFile ( AppleEvent *theAppleEvent, AEKeyword theAEKeyword, FSSpec *theFile, char *tempBuffer, long bufferSize )
  280. {
  281.     OSErr        theErr;
  282.     DescType    actualType;
  283.     Size        actualSize;
  284.     
  285.     my_assert ( theFile != NULL, "\plstarAEGetParamSpoolFile: theFile ptr is NULL" );
  286.     my_assert ( theAppleEvent != NULL, "\plstarAEGetParamSpoolFile: theAppleEvent ptr is NULL" );
  287.     my_assert ( tempBuffer != NULL, "\plstarAEGetParamSpoolFile: tempBuffer ptr is NULL" );
  288.     
  289.     /* put the message spool file name into the tempBuffer */
  290.     theErr = AEGetParamPtr
  291.         ( theAppleEvent, theAEKeyword, typeChar, &actualType, (Ptr)tempBuffer, bufferSize - 1, &actualSize );
  292.     if ( theErr == noErr )
  293.     {
  294.         my_assert ( actualSize < bufferSize, "\plstarAEGetParamSpoolFile: actual param size too big" );
  295.         
  296.         /* terminate the buffer with a null byte, making it a C format string */
  297.         tempBuffer[actualSize] = nil;
  298.         
  299.         /* convert to pascal format string */
  300.         C2PStr ( tempBuffer );
  301.         /* make an fsspec using the mail spool directory and the tempBuffer as file name */
  302.         theErr = FSMakeFSSpec ( gListSTARSpoolFolder.vRefNum,
  303.             gListSTARSpoolFolder.parID, (StringPtr)tempBuffer, theFile );
  304.     }
  305.     
  306.     return theErr;
  307. } /* lstarAEGetParamHTTPMethod */
  308.  
  309.  
  310. #endif    /* kCompileWithListSTARCode */
  311.  
  312. /***  EOF  ***/
  313.